

//
//  CircleView.m
//  Trace
//
//  Created by qianfeng on 16/3/17.
//  Copyright © 2016年 王卓越. All rights reserved.
//

#import "CircleView.h"
#import "UIImageView+WebCache.h"
//#import "UIImage+SDImageCacheExtension.h"
#import "DiscoverCircleModel.h"

@interface CircleView()<UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation CircleView


- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.circleDataSource = [[NSMutableArray alloc] init];
    }
    return self;
}

+ (instancetype)circleViewFrame:(CGRect)frame{
    CircleView *view = [[CircleView alloc] initWithFrame:frame];
    view.scrollView = [[UIScrollView alloc] initWithFrame:frame];
    
    view.scrollView.pagingEnabled = YES;

    [view addSubview:view.scrollView];
    
    view.timer = [NSTimer scheduledTimerWithTimeInterval:2.f target:view selector:@selector(TimerRun:) userInfo:nil repeats:YES];
    return view;
}


//重写setter方法

- (void)setCircleDataSource:(NSMutableArray *)circleDataSource{
    //在此必须把参数 circleDataSource 穿给该类的 全局变量 _circleDataSource ，因为下面的翻页会用到
    _circleDataSource = circleDataSource;
    self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.bounds)*circleDataSource.count, CGRectGetHeight(self.bounds));
    for (NSInteger i = 0; i < circleDataSource.count; i++) {
        _myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.bounds)*i, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
        
        _myImageView.userInteractionEnabled = YES;
        _myImageView.tag = 10 + i;
        
        
        DiscoverCircleModel *model = circleDataSource[i];
        [_myImageView sd_setImageWithURL:[NSURL URLWithString:model.cover]placeholderImage:[UIImage imageNamed:@"ting04"]];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.bounds)*i, self.myImageView.bounds.size.height-20, self.bounds.size.width, 20)];
        self.titleLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
        self.titleLabel.font = [UIFont systemFontOfSize:13];
        self.titleLabel.textColor = [UIColor whiteColor];

        self.titleLabel.text = model.title;
        [self.scrollView addSubview:_myImageView];
        [self.scrollView addSubview:_titleLabel];
    }
    [self createPageControl];
    
}



- (void)createPageControl{
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(200, 180, 100, 20)];
        _pageControl.numberOfPages = self.circleDataSource.count;
        _pageControl.currentPage = 0;
        _pageControl.alpha = 0.7;
        _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
        
        [_pageControl addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventEditingChanged];
        [self addSubview:_pageControl];
  
}
-(void)valueChange:(UIPageControl *)page{
  

}


//实现 定时器 的方法
- (void)TimerRun:(NSTimer *)timer{
    static NSInteger m;
    NSInteger n = self.scrollView.contentOffset.x / self.scrollView.bounds.size.width;
    //n=0,即不是最后一张
    if (n == 0) {
        m = 1;
    //n=self.circleDataSource.count - 1,即n是最后一张
    }else if (n == self.circleDataSource.count-1){
        m = -1;
    }
    n += m;
    
    [UIView animateWithDuration:0.5 animations:^{
    self.scrollView.contentOffset = CGPointMake(n * self.scrollView.bounds.size.width, 0);

    }completion:^(BOOL finished) {
        self.pageControl.currentPage = n;
    }];

}
@end
